<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\Cookie;
class Member extends Common
{
	public function _initialize(){
		header("Content-type: text/html; charset=utf-8");
        if (Cookie::has('userInfo')==false) {
            $this->error('对不起,您还没有登录或登录超时!请先登录', url('index/Login/login',['jump'=>'member']),'', 3);
        }
    }	
	
    public function index()
    {
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/profile/findProfile.jhtml?id=$memberId";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		if($res['code']==1){
			$data=$res['data'];
			return view('member/index',[
				'act'=>'member',
				'data'=>$data
			]);
		}else{
			$this->error('出错了,请稍后重试',url('index/Member/index'),'',3);				
		}	
	}
	public function passwordEdit(){
		
		return view('member/passwordedit');
	}
	public function orderList(){
		$pageNumber=1;
		$pageSize=10;
		$status='wait';
		if(input('?get.pageNumber')){
			$pageNumber=input('get.pageNumber');
		}
		if(input('?get.pageSize')){
			$pageSize=input('get.pageSize');
		}
		if(input('?get.status')){
			$status=input('get.status');
		}
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/profile/getOrdersByStatus.jhtml?memberId=$memberId&status=$status&pageSize=$pageSize&pageNumber=$pageNumber";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		if($res['code']==1){
			$data=$res['data']['array'];
			$totalPages=$res['data']['totalPages'];
			$pageNumber=$res['data']['pageNumber'];
			$total_arr=array();
			for($i=1; $i<=$totalPages; $i++){
				$total_arr[]=$i;
			}
			return view('member/orderlist',[
				'act'=>'member',
				'data'=>$data,
				'status'=>$status,
				'total_arr'=>$total_arr,
				'totalPages'=>$totalPages,
				'pageNumber'=>$pageNumber,
			]);
		}else{
			$this->error('出错了,请稍后重试',url('index/Member/index'),'',3);				
		}							
	}
	public function orderInfo(){
		$sn=input('get.sn');
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/profile/detail.jhtml?memberId=$memberId&sn=$sn";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		if($res['code']==1){
			$data=$res['data'];
			return view('order/orderInfo',['act'=>'index','data'=>$data]);
		}else{
			$this->error('出错了,请稍后重试',url('index/Member/index'),'',3);				
		}	   
	}
	public function userlist(){
		$pageNumber=1;
		$pageSize=10;
		if(input('?get.pageNumber')){
			$pageNumber=input('get.pageNumber');
		}
		if(input('?get.pageSize')){
			$pageSize=input('get.pageSize');
		}
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/profile/personalMemberList.jhtml?memberId=$memberId&pageSize=$pageSize&pageNumber=$pageNumber";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		if($res['code']==1){
			$data=$res['data']['array'];
			$totalPages=$res['data']['totalPage'];
			$pageNumber=$res['data']['pageNumber'];
			$total_arr=array();
			for($i=1; $i<=$totalPages; $i++){
				$total_arr[]=$i;
			}
			return view('member/userlist',[
				'act'=>'member',
				'data'=>$data,
				'total_arr'=>$total_arr,
				'totalPages'=>$totalPages,
				'pageNumber'=>$pageNumber,
			]);
		}else{
			$this->error('出错了,请稍后重试',url('index/Member/index'),'',3);				
		}			
	}
	public function addUser(){
		$name=urlencode(input('post.name'));
		$phone=input('post.phone');
		$isEnable=input('post.isEnable');
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/profile/addMember.jhtml?memberId=$memberId&name=$name&mobile=$phone&isEnable=$isEnable";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		echo json_encode($res);
	}
	public function detailUser(){
		$id=input('post.id');
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/profile/getDetail.jhtml?memberId=$memberId&childId=$id";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		echo json_encode($res);		
	}	
	public function editUser(){
		$id=input('post.id');
		$name=urlencode(input('post.name'));
		$phone=input('post.phone');
		$isEnable=input('post.isEnable');
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/profile/update.jhtml?memberId=$memberId&childId=$id&name=$name&mobile=$phone&isEnable=$isEnable";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		echo json_encode($res);		
	}
	public function rebate(){
		$pageNumber=1;
		$pageSize=10;
		if(input('?get.pageNumber')){
			$pageNumber=input('get.pageNumber');
		}
		if(input('?get.pageSize')){
			$pageSize=input('get.pageSize');
		}
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/profile/getPointRecord.jhtml?memberId=$memberId&pageSize=$pageSize&pageNumber=$pageNumber";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		if($res['code']==1){
			$data=$res['data']['array'];
			$totalPages=$res['data']['totalPages'];
			$pageNumber=$res['data']['pageNumber'];
			$total_arr=array();
			for($i=1; $i<=$totalPages; $i++){
				$total_arr[]=$i;
			}
			return view('member/rebate',[
				'act'=>'member',
				'data'=>$data,
				'total_arr'=>$total_arr,
				'totalPages'=>$totalPages,
				'pageNumber'=>$pageNumber,
			]);
		}else{
			$this->error('出错了,请稍后重试',url('index/Member/index'),'',3);				
		}
	}
	public function userEdit(){
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/profile/getPersonalData.jhtml?memberId=$memberId";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		if($res['code']==1){
			$data=$res['data'];
			return view('member/useredit',[
				'act'=>'member',
				'data'=>$data
			]);
		}else{
			$this->error('出错了,请稍后重试',url('index/Member/index'),'',3);				
		}			
	}
	public function pwdEdit(){
		$password=input('post.password');
		$password=md5($password);
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/profile/updatePassword.jhtml?memberId=$memberId&password=$password";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		echo json_encode($res);		
	}
	public function integral()
	{
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);			
		$url=config('tdwApi')."/product/getPointProducts.jhtml";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		if($res['code']==1){
			$data=$res['data'];
			return view('member/integral',['data'=>$data]);
		}else{
			$this->error('出错了请稍后重试',url('index/Member/index'),'',3);				
		}
	}
	public function updatePoint(){
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/profile/getPoint.jhtml?memberId=$memberId";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		echo json_encode($res);		
	}
	public function integralMember(){
		$pageNumber=1;
		$pageSize=10;
		if(input('?get.pageNumber')){
			$pageNumber=input('get.pageNumber');
		}
		if(input('?get.pageSize')){
			$pageSize=input('get.pageSize');
		}
		$userInfo=Cookie::get('userInfo');
		Cookie::set('userInfo',$userInfo,7200);
		$memberId=$userInfo['id'];			
		$url=config('tdwApi')."/exchangeRecord/list.jhtml?memberId=$memberId&pageSize=$pageSize&pageNumber=$pageNumber";
		$res=$this->http($url);
		$res=json_decode($res[0],true);	
		if($res['code']==1){
			$data=$res['data']['array'];
			$totalPages=$res['data']['totalPages'];
			$pageNumber=$res['data']['pageNumber'];
			$total_arr=array();
			for($i=1; $i<=$totalPages; $i++){
				$total_arr[]=$i;
			}
			return view('member/integralMember',[
				'act'=>'member',
				'data'=>$data,
				'total_arr'=>$total_arr,
				'totalPages'=>$totalPages,
				'pageNumber'=>$pageNumber,
			]);
		}else{
			$this->error('出错了,请稍后重试',url('index/Member/index'),'',3);				
		}
	}
	
	
	

}
